home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11044 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: news.sinet.slb.com!usenet
  2. From: "Vinh D. Nguyen" <vnguyen@sugar-land.anadrill.slb.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: pointer to 2 dim array
  5. Date: Tue, 12 Mar 1996 07:59:27 -0600
  6. Organization: Schlumberger Anadrill
  7. Message-ID: <3145833F.740C@sugar-land.anadrill.slb.com>
  8. References: <313B3317.3CD9@gold.interlog.com> <Dnv24o.F2B@mv.mv.com>
  9. NNTP-Posting-Host: 163.185.118.40
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (WinNT; I)
  14.  
  15. Michael Furman wrote:
  16. > In article <313B3317.3CD9@gold.interlog.com>, markg@gold.interlog.com
  17. > says...
  18. > >
  19. > >How can I create a dynamic 2 dimesional array
  20. > >and be able to be store and retrieve from it?
  21. > int (x *) [100] = new int[200][100];
  22. > >
  23. > >Thanks.
  24. > >--
  25. > --
  26. > <<< If you received it by E-mail: it is a copy of post to the newsgroup
  27. > >>>
  28. > ---------------------------------------------------------------
  29. > Michael Furman,                       (603)893-1109
  30. > Geophysical Survey Systems, Inc.  fax:(603)889-3984
  31. > 13 Klein Drive - P.O. Box 97          engr@gssi.mv.com
  32. > North Salem, NH 03073-0097            71543.1334@compuserve.com
  33. > ---------------------------------------------------------------
  34.  
  35. The above approach requires that one dimension is fixed, in this case all rows
  36. will have length of 100. A more dynamic approach is illustrated in the 
  37. following C function:
  38.  
  39. int ** 2DAlloc( int nRows, int nCols )
  40. {
  41.     int ** ppArray = new int * [ nCols ];
  42.     for( int i = 0; i < nCols; i++ )
  43.         ppArray[ i ] = new int [ nRows ];
  44.  
  45.     return ppAray;
  46. }
  47.  
  48. After receiving the pointer from 2DAlloc, you can access the array entries 
  49. using the standard array indexing syntax.
  50.  
  51. To deallocate the array, use the following function:
  52.  
  53. void 2DFree( int ** ppArray, int nRows )
  54. {
  55.     for( int i; i < nRows; i++ )
  56.         delete [] ppArray[ i ];
  57.     delete [] ppArray;
  58. }
  59.  
  60. Of course, you can also define a C++ class to encapsulate a dynamic 2D array.
  61.  
  62. --------------------------------------------------------------------------
  63. * Vinh Nguyen                                            vnguyen@slb.com *
  64. * Drilling Information Products - Senior Engineer                        *
  65. * Anadrill Schlumberger                             *
  66. * 200 Gillingham Ln.                             (713) 275-7524 (Office) *
  67. * Sugarland, TX 77478                            (713) 275-8098 (FAX)    *
  68. --------------------------------------------------------------------------
  69.